package com.wigball.java.jchef;

import java.io.File;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.wigball.java.jchef.parser.Parser;

public final class JChef {
	
	private static final String OPTION_SCRIPT = "s";
	
	//private static final String OPTION_DEBUG = "debug";
	
	private static final Options CLI_OPTIONS = new Options();
	static {
		Option scriptOption = new Option(OPTION_SCRIPT, "script-file", true, "The chef script file to parse");
		scriptOption.setRequired(true);
		scriptOption.setArgName("file");
		
		//Option debugOption = new Option(OPTION_DEBUG, "Print debug output");
		
		CLI_OPTIONS.addOption(scriptOption);
		//CLI_OPTIONS.addOption(debugOption);
	}
	
	private JChef() {
		
	}
	
	public static void main(String[] args) {
		CommandLineParser parser = new BasicParser();
		try {
			CommandLine cmd = parser.parse(CLI_OPTIONS, args);
			
			String filename = cmd.getOptionValue(OPTION_SCRIPT);
			File f = new File(filename);
			
			if (!f.exists() || !f.isFile() || !f.canRead()) {
				System.out.println("Could not access " + filename);
				System.exit(1);
			}
			
			Parser chefParser = Parser.getInstance();
			chefParser.parse(f);
			
		} catch (ParseException e) {
			//System.out.println(e.getClass().getSimpleName() + " while parsing CMD arguments: " + e.getMessage());
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp("JChef", CLI_OPTIONS, true);
		}
	}
	
}
